package com.yricky.oh_info

/**
 * @author  Yricky
 * @date  2021/10/5 下午8:07
 */
object OhUtils {
    const val ERR_NO_RUNTIME3 = "Not a device with open harmony runtime 3+ !"

    private fun <T:Any?> getMValue(clazz:Class<*>?, funName: String):T?{
        if(clazz == null)
            return null
        val method = clazz.getMethod(funName)
        return try{
            method.invoke(null) as T
        }catch (e:Throwable){
            null
        }
    }


    object SystemVersion{
        val clazz by lazy{
            try{
                Class.forName("ohos.system.version.SystemVersion")
            }catch (e:Throwable){
                null
            }
        }

        val apiVersion:Int get() = getMValue(clazz,"getApiVersion") ?: -1

        val buildVersion:Int get() = getMValue(clazz,"getBuildVersion") ?: -1

        val featureVersion:Int get() = getMValue(clazz,"getFeatureVersion") ?: -1

        val majorVersion:Int get() = getMValue(clazz,"getMajorVersion") ?: -1

        val version:String get()= getMValue(clazz,"getVersion") ?: ERR_NO_RUNTIME3

        val releaseType:String get()= getMValue(clazz,"getReleaseType")?: ERR_NO_RUNTIME3

        val seniorVersion:Int get()= getMValue(clazz,"getSeniorVersion") ?: -1
    }

    object DeviceInfo{
        val clazz by lazy{
            try{
                Class.forName("ohos.system.DeviceInfo")
            }catch (e:Throwable){
                null
            }
        }

        val deviceType:String = getMValue(clazz,"getDeviceType") ?: ERR_NO_RUNTIME3

        val productModel:String = getMValue(clazz,"getModel") ?: ERR_NO_RUNTIME3

        val name:String = getMValue(clazz,"getName") ?: ERR_NO_RUNTIME3

        val locale:String = getMValue(clazz,"getLocale") ?: ERR_NO_RUNTIME3
    }
}